/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.utils.excel.ExportExcel;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.PersonStatistic;
import com.fosung.frame.modules.approve.service.DzbStatisticService;
import com.fosung.frame.modules.approve.service.StatisticService;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.life.service.OrganizeLifeService;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.score.entity.OrganizePersonDetail;
import com.fosung.frame.modules.score.service.OrganizePersonDetailService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 党员统计Controller
 * 
 * @author ygl
 * @version 2017-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/statisticPerson")
public class StatisticPersonController extends BaseController {

	@Resource
	private StatisticService statisticService;
	@Resource
	private OrganizePersonDetailService organizePersonDetailService;
	@Resource
	private OrganizeLifeService organizeLifeService;
	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;
	@Autowired
	private DzbStatisticService dzbStatisticService;

	@RequiresPermissions("approve:statisticPerson:view")
	@RequestMapping(value = { "list", "" })
	public String list(PersonStatistic personStatistic, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		if (personStatistic.getZq() == null) {
			personStatistic.setZq("2017");
		}
		// List<PersonStatistic> list =
		// statisticService.findPersonByOrganize(personStatistic);
		personStatistic.setOrganizationId(UserUtils.getUser().getOffice().getId());
		List<PersonStatistic> list = dzbStatisticService.findPersonPmList(personStatistic);
		model.addAttribute("PersonStatisticList", list);
		model.addAttribute("zq", personStatistic.getZq());
		return "modules/approve/statisticPersonList";
	}

	@RequestMapping(value = "form")
	public String form(String id, Model model) {
		List<String> ids = new ArrayList();
		List<OrganizePersonDetail> idList = organizePersonDetailService.findDistinctBYid(id);
		for (int i = 0; i < idList.size(); i++) {
			ids.add(idList.get(i).getOrganizeLifeId());
		}
		OrganizeLife o = new OrganizeLife();
		o.setIdList(ids);
		List<OrganizeLife> organizeLifeList = organizeLifeService.findList(o);

		if (organizeLifeList.size() > 0) {
			for (int i = 0; i < organizeLifeList.size(); i++) {
				StringBuffer typeNames = new StringBuffer();
				OrganizeLife o1 = organizeLifeList.get(i);
				if (o1.getTypeids() != null && !o1.getTypeids().equals("")) {
					String[] types = o1.getTypeids().split(",");

					if (types != null && types.length > 0) {
						for (String s : types) {
							// 循环遍历数组中元素，添加到StringBuilder 对象中
							String t = organizeLifeTypeService.get(s).getTypename();
							typeNames.append(t + ",");
						}
					}
					if (typeNames.length() > 0) {
						// 调用字符串的deleteCharAt()方法,删除最后一个多余的逗号
						typeNames.deleteCharAt(typeNames.length() - 1);
					}

				}
				o1.setTypeids(typeNames.toString());
			}
		}

		model.addAttribute("organizeLifeList", organizeLifeList);
		return "modules/approve/personStatisticFormById";
	}

	/**
	 * 查询党员排名
	 * 
	 * @param personStatistic
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "findPersonPmList")
	@ResponseBody
	public List<PersonStatistic> findPersonPmList(PersonStatistic personStatistic, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		personStatistic.setNum(10);
		personStatistic.setOrganizationId(UserUtils.getUser().getOffice().getId());
		List<PersonStatistic> list = dzbStatisticService.findPersonPmList(personStatistic);
		return list;
	}

	// 导出Excel
	@RequestMapping(value = "export", method = RequestMethod.GET)
	public String exportFile(PersonStatistic personStatistic, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		String fileName = "";
		try {
			if (personStatistic.getZq() == null) {
				personStatistic.setZq("2017");
			}
			personStatistic.setOrganizationId(UserUtils.getUser().getOffice().getId());
			List<PersonStatistic> Excellist = dzbStatisticService.findPersonPmList(personStatistic);

			if (fileName == "") {
				fileName = personStatistic.getZq() + "年度党员积分统计.xlsx";
			}

			new ExportExcel(fileName, PersonStatistic.class).setDataList(Excellist).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出党员积分统计失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/approve/statisticPerson/list?repage";
	}

}